package ute.mvc.service;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import ute.mvc.dao.SubmittedTestAndQuizzDAO;
import ute.mvc.dto.NextData;
import ute.mvc.dto.QuestionDTO;
import ute.mvc.dto.TestDTO;
import ute.mvc.model.SubmittedTestAndQuizz;

@Service("submittedTestAndQuizzService")
public class SubmittedTestAndQuizzServiceImpl implements
		SubmittedTestAndQuizzService {

	@Autowired
	SubmittedTestAndQuizzDAO submittedTestAndQuizzDAO;

	@Transactional
	public void addSubmittedTestAndQuizz(
			SubmittedTestAndQuizz submittedTestAndQuizz) {
		submittedTestAndQuizzDAO
				.addSubmittedTestAndQuizz(submittedTestAndQuizz);
	}

	@Transactional
	public void updateSubmittedTestAndQuizz(
			SubmittedTestAndQuizz submittedTestAndQuizz) {
		submittedTestAndQuizzDAO
				.updateSubmittedTestAndQuizz(submittedTestAndQuizz);
	}

	@Transactional
	public void deleteSubmittedTestAndQuizz(
			SubmittedTestAndQuizz submittedTestAndQuizz) {
		submittedTestAndQuizzDAO
				.deleteSubmittedTestAndQuizz(submittedTestAndQuizz);
	}

	public SubmittedTestAndQuizz getSubmittedTestAndQuizz(String id) {
		// TODO Auto-generated method stub
		return submittedTestAndQuizzDAO.getSubmittedTestAndQuizz(id);
	}

	public QuestionDTO getNextQuestion(NextData data) {
		// TODO Auto-generated method stub
		int number = data.getQuestionNumber();
		String submittedTestId = data.getTestId();

		// Get data
		ObjectMapper map = new ObjectMapper();
		String json = submittedTestAndQuizzDAO.getSubmittedTestAndQuizz(
				submittedTestId).getExamQuestion();
		TestDTO testDTO = null;
		try {
			testDTO = map.readValue(json, TestDTO.class);
		} catch (JsonParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// Get Question
		int k = 0;
		for (int i = 0; i < testDTO.getListPart().size(); i++) {
			for (int j = 0; j < testDTO.getListPart().get(i).getListQuestion()
					.size(); j++) {
				if (k == number) {
					return testDTO.getListPart().get(i).getListQuestion()
							.get(j);
				}
				k++;
			}
		}
		return null;
	}

	@SuppressWarnings("null")
	public SubmittedTestAndQuizz pointTest(SubmittedTestAndQuizz submitted) {
		// TODO Auto-generated method stub
		ObjectMapper map = new ObjectMapper();
		String json = submitted.getExamQuestion();
		String userAnswer = submitted.getAnswerOfStudent();
		String[] answers = userAnswer.split(";");
		TestDTO testDTO = null;
		try {
			testDTO = map.readValue(json, TestDTO.class);
		} catch (JsonParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		float point = 0;
		for (int i = 0; i < answers.length; i++) {
			String[] q = answers[i].split("<>");
			int count = 0;
			for (int j = 0; j < testDTO.getListPart().size(); j++) {
				for (int k = 0; k < testDTO.getListPart().get(j)
						.getListQuestion().size(); k++) {
					// Update question user had chosen
					QuestionDTO dto = null;
					dto = testDTO.getListPart().get(j).getListQuestion().get(k);
					if (count == i) {
						if (q.length > 2) {
							if (dto.getRightAnswer().equals(q[2])) {
								point += dto.getPoint();
								dto.setCorrect(true);
							}
							dto.setRightAnswer(q[2]);
						} else {
							dto.setRightAnswer("");
						}
					}
					// Hadn't chosen
					if (count >= answers.length) {
						dto.setRightAnswer("");
					}
					count++;
				}
			}
		}
		try {
			json = map.writeValueAsString(testDTO);
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		submitted.setPoint(point);
		submitted.setExamQuestion(json);
		return null;
	}

	public int getInCompleteSubmittedTestAndQuizz(String id) {
		// TODO Auto-generated method stub
		SubmittedTestAndQuizz test = submittedTestAndQuizzDAO
				.getSubmittedTestAndQuizz(id);
		if (test == null)
			return -1;
		String answer = test.getAnswerOfStudent();
		if ("".equals(answer))
			return 0;
		String json = test.getExamQuestion();
		String[] answers = answer.split(";");
		ObjectMapper map = new ObjectMapper();
		TestDTO testDTO = null;

		try {
			testDTO = map.readValue(json, TestDTO.class);
		} catch (JsonParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// Count question in test
		int count = 0;
		for (int i = 0; i < testDTO.getListPart().size(); i++) {
			count += testDTO.getListPart().get(i).getListQuestion().size();
		}

		System.out.println(answers.length);
		for (String a : answers)
			System.out.println(a);
		// Done
		if (answers.length == count)
			return -1;
		// Question Number
		return answers.length;
	}

	public int getNumberQuestion(String id) {
		// TODO Auto-generated method stub
		SubmittedTestAndQuizz test = submittedTestAndQuizzDAO
				.getSubmittedTestAndQuizz(id);
		if (test == null)
			return -1;

		String lstQuestion = test.getExamQuestion();

		ObjectMapper map = new ObjectMapper();
		TestDTO testDTO = null;

		try {
			testDTO = map.readValue(lstQuestion, TestDTO.class);
		} catch (JsonParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		int count = 0;
		for (int i = 0; i < testDTO.getListPart().size(); i++) {
			count += testDTO.getListPart().get(i).getListQuestion().size();
		}
		return count;
	}

}
